package org.qiaice.service.impl;

import org.qiaice.service.CommonService;
import org.qiaice.util.OSSUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

@Service
public class CommonServiceImpl implements CommonService {

    @Override
    public String upload(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String suffix = Objects.isNull(originalFilename) ? "" : OSSUtil.getFileSuffix(originalFilename);
        String filename = UUID.randomUUID() + suffix;
        try {
            filename = OSSUtil.upload(file.getInputStream(), filename);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
        return filename;
    }
}
